﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace Dataglot
{
    /// <summary>
    /// Manage some connection and transaction tasks for a class that sometimes must share a connection.
    /// </summary>
    /// <remarks>
    /// This is class that will do some of the repetitive connection and transaction management and 
    /// implement a dispose pattern that either aggressively disposes of objects at end of life, or
    /// leaves connections and transactions alone in shared scenarios.
    /// 
    /// This differs from a GenericCommand in that the derived classes will probably use multiple
    /// GenericCommands.
    /// </remarks>
    public class ConnectionConsumer:IDisposable
    {
        private readonly bool needToManageConnection;
        protected readonly DataFactory factory;
        protected DbConnection con { get; set; }
        private TransactionManager transactionManager;
        protected DbTransaction Transaction
        {
            get { return transactionManager.Transaction; }
        }
        public ConnectionConsumer(DataFactory targetFactory)
        {
            needToManageConnection = true;
            factory = targetFactory;
        }


        public ConnectionConsumer(DataFactory sharedFactory, DbConnection sharedConnection, TransactionManager sharedTransactionManager)
        {
            if(sharedFactory==null)
                throw new ArgumentNullException("sharedFactory","Data factory is null");
            if (sharedConnection == null)
                throw new ArgumentNullException("sharedConnection", "sharedConnection is null");

            needToManageConnection = false;
            factory = sharedFactory;
            con = sharedConnection;
            transactionManager = sharedTransactionManager;
        }

        protected void OpenConnection()
        {
            if(needToManageConnection)
            {
                if (con == null)
                {
                    con = factory.CreateOpenConnection();
                }
            }
            else
            {
                if(con==null)
                {
                    throw new InvalidOperationException("Expected non-null connection, but was null");
                }

                if (con.State != ConnectionState.Open)
                {
                    throw new InvalidOperationException("Expected open connection, but it was wasn't");
                }
            }
        }

        protected void BeginTransaction()
        {
            if (needToManageConnection)
            {
                if (con == null)
                    throw new InvalidOperationException("connection required to start a transaction");

                transactionManager = new TransactionManager(con);
                transactionManager.Begin();
            }
        }

        protected void CommitIfRequired()
        {
            if (needToManageConnection)
            {
                transactionManager.Commit();
            }
        }

        public void Dispose()
        {
            if(needToManageConnection)
            {
                if (transactionManager != null)
                    transactionManager.Dispose();
                if (con != null)
                    con.Dispose();
            }
        }
    }
}
